package com.hziee.graduate.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.RegionDao;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.Region;
import com.hziee.graduate.ticket.service.RegionService;
import com.hziee.graduate.ticket.utils.BeanConvertUtil;
import com.hziee.graduate.ticket.vo.AreaVO;
import com.hziee.graduate.ticket.vo.RegionVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("regionService")
public class RegionServiceImpl extends ServiceImpl<RegionDao, Region> implements RegionService {
    @Resource
    private RegionDao regionDao;

    @Override
    @Transactional
    public R<List<Region>> loadRegionVOByPid(Integer pid) {
        // 根据DO获得VO
        QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<Region>().select().eq("pid", pid).eq("del_flag",0);
        List<RegionVO> regionVOByPid = convertToRegionVO(regionQueryWrapper);
        // 填充VO的children属性
        List<RegionVO> regionVOS = populateChildren(regionVOByPid);
        return R.success(regionVOS);
    }

    @Override
    public R<List<Region>> getRegionVOByLevel(Integer level) {
        // 根据DO获得VO
        QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<Region>().select().eq("level", level);
        List<RegionVO> regionVOByLevel = convertToRegionVO(regionQueryWrapper);
        // 填充VO的children属性
        List<RegionVO> regionVOS = populateChildren(regionVOByLevel);
        return R.success(regionVOS);
    }

    @Override
    public R<List<AreaVO>> getAreaByCity(String locationName) {
        List<AreaVO> areaByCity = regionDao.getAreaByCity(locationName);
        return R.success(areaByCity);
    }

    /*
     * @description: 获得RegionVO
     * @author: huwanxiao
     * @date: 2022/9/7 19:22
     **/
    private List<RegionVO> convertToRegionVO(QueryWrapper<Region> qw) {
        // 搜索到该级数据并转换为VO
        List<Region> sourceList = regionDao.selectList(qw);
        List<RegionVO> RegionVoList = BeanConvertUtil.converList(sourceList, RegionVO.class);
        return RegionVoList;
    }

    /*
     * @description: 对VO进行遍历,填充children和hasChildren
     * @author: huwanxiao
     * @date: 2022/9/7 19:19
     **/
    private List<RegionVO> populateChildren(List<RegionVO> RegionVOs) {
        for (RegionVO regionVO : RegionVOs) {
            // 根据id
            QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<Region>().select().eq("pid", regionVO.getId()).eq("del_flag",0);
            List<RegionVO> regions = convertToRegionVO(regionQueryWrapper);
            boolean hasChildren = regions.size() > 0 ? true : false;
            regionVO.setHasChildren(hasChildren);
            regionVO.setChildren(regions);
        }
        return RegionVOs;
    }
}

